package com.web.controller.plumber;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.common.apiresult.CommonResult;
import com.common.domain.dto.MaintainDTO;
import com.common.domain.dto.MaintainQueryDTO;
import com.service.serviceInterface.MaintainService;
import com.service.utils.SecurityUtil;
import com.system.entity.Maintain;
import com.web.entity.dto.PlumberUpdateRepairDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @author liuxuhuang
 * @date 2023年04月04日 11:37
 * @desc 宿舍财产
 */
@RestController(value = "plumberDormPropertyController")
@Api(value = "dormProperty", tags = "宿舍财产控制器")
@RequestMapping(value = "/plumber/dormProperty")
@Slf4j
public class DormPropertyController {
    @Resource
    private MaintainService maintainService;
    @Resource
    private SecurityUtil securityUtil;

    @GetMapping
    @ApiOperation(value = "获取维修申请单")
    public CommonResult getMaintains(MaintainQueryDTO dto) {
        return maintainService.getMainTainsDataByPlumber(dto);
    }

    @PutMapping
    @ApiOperation(value = "修改维修申请单信息")
    public CommonResult updateMaintain(@RequestBody @Validated PlumberUpdateRepairDTO dto) {
        long userId = securityUtil.getUserId(SecurityContextHolder.getContext());
        LocalDateTime parse = LocalDateTime.parse(dto.getHandleTime(), DateTimeFormatter.ISO_DATE_TIME);
        boolean update = maintainService.update(new LambdaUpdateWrapper<Maintain>().set(Maintain::getFeedback, dto.getFeedback())
                .set(Maintain::getHandleWorkId, userId).set(Maintain::getHandleTime, parse).eq(Maintain::getId, dto.getId()));
        return update ? CommonResult.success() : CommonResult.failed();
    }

    @PostMapping
    @ApiOperation(value = "新增所在宿舍财产维修申请单")
    public CommonResult addMaintain(@RequestBody MaintainDTO dto) {
        long userId = securityUtil.getUserId(SecurityContextHolder.getContext());
        return maintainService.add(dto, userId);
    }
}
